package com.jinzecloud.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.blog.bean.Article;
import com.jinzecloud.blog.mapper.ArticleMapper;
import com.jinzecloud.blog.service.ArticleService;
import com.jinzecloud.blog.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        articleMapper.insert(article);
    }

    @Override
    public void updateArticle(Article article) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("a_id",article.getAId());
        articleMapper.update(article,wrapper);
    }

    @Override
    public void deleteArticle(Long[] ids) {
        articleMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Article getByIdArticle(Long id) {
        Article article = articleMapper.selectById(id);
        return article;
    }

    @Override
    public Page<Article> getAllArticle() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        List<Article> articles = articleMapper.selectList(wrapper);
        Page<Article> page = new Page<>(articles,0,0,0);
        return page;
    }

    @Override
    public void updateArticleLikecount(Article article) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("a_id",article.getAId());
        articleMapper.update(article,wrapper);
    }

    @Override
    public void updateArticleCommentcount(Article article) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("a_id",article.getAId());
        articleMapper.update(article,wrapper);
    }

    @Override
    public void updateArticleReadcount(Article article) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("a_id",article.getAId());
        articleMapper.update(article,wrapper);
    }

    @Override
    public void updateArticleTopflag(Article article) {
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("a_id",article.getAId());
        articleMapper.update(article,wrapper);
    }
}
